package com.lanou.sfu.songforu.mv;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.lanou.sfu.songforu.R;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MVFragmentAdapter extends BaseAdapter {
    private List<TTMV.DataEntity> datas;
    private Context mContext;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;


    public MVFragmentAdapter(Context mContext, List<TTMV.DataEntity> s) {
        this.mContext = mContext;
        datas = s;
        requestQueue = Volley.newRequestQueue(mContext);
        imageLoader = new ImageLoader(requestQueue, new MyImageLoader());

    }


    public void addDatas(List<TTMV.DataEntity> s) {
        datas.addAll(s);
        notifyDataSetChanged();
    }

    public void refreshDatas(List<TTMV.DataEntity> s) {
        datas = s;
        notifyDataSetChanged();
    }





    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 初始化缓存
        viewHolder holder = null;
        if (convertView == null) {
            // 加载行布局
            convertView = LayoutInflater.from(mContext).inflate(R.layout.hot_mv_item,null);
            holder = new viewHolder();
            holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
            holder.aliastitle = (TextView) convertView.findViewById(R.id.aliastitle);
            holder.artist = (TextView) convertView.findViewById(R.id.artist);
            holder.play_nums = (TextView) convertView.findViewById(R.id.play_nums);

            convertView.setTag(holder);
        }  else {
            holder = (viewHolder) convertView.getTag();
        }
        // 设置属性
        TTMV.DataEntity result = datas.get(position);
        String image = result.getPicUrl();
        if (image != null) {
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(holder.thumbnail, R.mipmap.loading, R.mipmap.loading);
            imageLoader.get(image, imageListener);
            holder.aliastitle.setText(result.getVideoName());
            holder.artist.setText(result.getSingerName());
            holder.play_nums.setText(result.getPickCount() + "");
        }

        return convertView;
    }



    public class viewHolder {
        private ImageView thumbnail;
        private TextView aliastitle;
        private TextView artist;
        private TextView play_nums;




    }


    // 拉取图片方法

    class MyImageLoader implements ImageLoader.ImageCache {

        private LruCache<String, Bitmap> cache;

        public MyImageLoader() {
            super();

            cache = new LruCache<String, Bitmap>(20 * 1024 * 1024) {
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }

        @Override
        public Bitmap getBitmap(String s) {
            return cache.get(s);
        }

        @Override
        public void putBitmap(String s, Bitmap bitmap) {
            cache.put(s, bitmap);
        }

    }
}